import { _decorator, AssetManager, assetManager, Component, director, Label, Material, Node, ProgressBar, sys, Vec3 } from 'cc';
import { MonoUIPanel } from './MonoUIPanel';
import { MonoBehavior } from './MonoBehavior';
import { LocalConfig } from '../../../bundle/framework/csv/LocalConfig';
import { GGAudio } from '../../../bundle/framework/core/base/GGAudio';
const { ccclass, property } = _decorator;

@ccclass('SplashScene')
export class SplashScene extends MonoBehavior {

    @property( {type: ProgressBar, displayName: "进度"} )
    progress: ProgressBar = null;

    @property( {type: Label, displayName: "进度标签"} )
    currentPro: Label = null;

    @property( {type: Label, displayName: "加载中"} )
    loadingText: Label = null;

    @property( {type: Node, displayName: "进度图标"} )
    iconRun: Node = null;
    private loadStartTime: number = 0
    private bundleVer: string

    // 进度
    private proIndex = 0
    private nexProIndex = 0
    private timeIndex = 0

    async onStart(): Promise<void> {

        this.initValues()
        
        await this.loadConfigsBundles()
        this.alignProgress( 25 )
        await this.loadTables()
        this.alignProgress( 50 )
        this.alignProgress( 100 )
        this.loadBundleConfig()
    }

    update(dt: any): void {
        if (this.proIndex <= this.nexProIndex) {
            this.progress.progress = this.proIndex / 100.0
            this.currentPro.string = Math.floor(this.proIndex) + "%"
            this.proIndex += 1

            const pos = this.iconRun.position

            this.iconRun.position = new Vec3(-250 + 500 * this.progress.progress, pos.y, pos.z)
        }

        this.timeIndex++
        const dots = Math.floor( this.timeIndex / 30 ) % 4
        if (dots == 0) 
            this.loadingText.string = "加载中"
        else if (dots == 1)
            this.loadingText.string = "加载中."
        else if (dots == 2)
            this.loadingText.string = "加载中.."
        else if (dots == 3)
            this.loadingText.string = "加载中..."
    }

    private initValues() {
        gg.log('StartScene Init Value');

        this.loadStartTime = Date.now();
        this.bundleVer = sys.localStorage.getItem('__bundleVersionConfig__');
    }

    private alignProgress(value: number) {
        this.nexProIndex = value
        if (this.proIndex < this.nexProIndex) {
            // this.proIndex = this.nexProIndex
            
            this.progress.progress = this.proIndex / 100.0
            this.currentPro.string = Math.floor(this.proIndex) + "%"
        }
    }

    // 加载表格包
    async loadConfigsBundles() {
        await new Promise<any>((resolve, reject) => {

            assetManager.loadBundle('tables', { version: this.bundleVer }, (err, bundle) => {
                if (err) { return resolve('err') }
    
                LocalConfig.instance.loadConfig(() => {
                    return resolve("");
                }, "tables");
            });
        });
    }
    
    // 加载各个表格
    async loadTables() {
        await new Promise<any>((resolve, reject) => {
            LocalConfig.instance.loadConfig(() => {
                this.scheduleOnce(() => {

                    return resolve("");
                });
            }, "tables");
        });
    }

    // 加载各个图片资源
    async loadTextures() {
        await new Promise<any>((resolve, reject) => {

            assetManager.loadBundle('textures', { version: this.bundleVer }, (err, bundle) => {
                if (err) { return resolve('err') }
    
                return resolve("");
            });
        });
    }

    async preloadViews() {
        
    }
    
    private loadBundleConfig() {
        gg.log('StartScene loadBundleConfig');
        var self = this

        assetManager.loadBundle('start', { version: this.bundleVer }, (err, bundle) => {
            if (err) { return gg.error(err); }
            
            self.alignProgress( 100 )
            self.proIndex = 100
            self.progress.progress = self.proIndex / 100.0
            self.currentPro.string = Math.floor(self.proIndex) + "%"


            if (director.getScene().getChildByName("__audioMgr__") == null) {
                GGAudio.init();
            }
            
            bundle.loadScene('start', (err, scene) => {
                if (err) { return gg.error(err); }
                console.log("run start time: ", Math.floor((Date.now() - this.loadStartTime) / 1000));
                director.runScene(scene);
            });
        });
        
    }

    // ---------------------- 点击事件 ---------------------- //
    onSetClicked() {
        console.log ("点击设置")
    }

    onLocationClicked() {
        console.log ("点击位置")
    }

    onRankingClicked() {
        console.log ("点击排行榜")
    }

    onSkinClicked() {
        console.log ("点击皮肤")
    }

    onStartClicked() {
        console.log ("点击凯斯")

        this.loadBundleConfig()
    }
}
